package org.fage.semples.service;

import java.io.InputStream;
import java.util.ArrayList;

import java.util.List;


import org.apache.poi.ss.usermodel.Workbook;
import org.fage.semples.dao.ExamUserHiberanteDAO;
import org.fage.semples.dao.ExamUserRoleHiberanteDAO;
import org.fage.semples.model.ExamUser;
import org.fage.semples.model.ExamUserRole;
import org.fage.semples.model.ExamUserVO;
import org.fage.semples.model.PagerModel;
import org.fage.semples.util.ExcelUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class ExamUserHiberanteServiceImpI implements ExamUserHiberanteService {

	@Autowired
	private ExamUserHiberanteDAO examUserHiberanteDAO;
	
	@Autowired
	private ExamUserRoleHiberanteDAO examUserRoleHiberanteDAO;
	
	@Override
	public PagerModel<ExamUser> findAllByPage() {
		return examUserHiberanteDAO.findAllByPage();
	}
	
	@Override
	public List<ExamUser> findAllExamUsers() {
		return examUserHiberanteDAO.findAllExamUsers();
	}

	@Override
	public ExamUser getExamUserById(String userId) throws Exception {
		return examUserHiberanteDAO.getExamUserById(userId);
	}

	@Override
	public void editExamUser(ExamUser examUser) throws Exception {
		examUserHiberanteDAO.editExamUser(examUser);
	}

	@Override
	public void createExamUser(ExamUser examUser) throws Exception {
		examUserHiberanteDAO.createExamUser(examUser);
	}

	@Override
	public void deleteExamUser(String userId) throws Exception {
		examUserHiberanteDAO.deleteExamUser(userId);
	}

	//批量添加excel内的学生
	@Override
	public void createExamUserList(InputStream inputStream, String postfix) throws Exception {
		Workbook wk = ExcelUtils.openWorkbook(inputStream, postfix);
		List<ExamUserVO> examUserVOList = ExcelUtils.getStudentListByExcel(wk);
//		List<ExamUser> examUserList = new ArrayList<ExamUser>();
		if(examUserVOList != null){
//			ExamRole role = examRoleDAO.get(25);
			ExamUser examUser = null;
			for(int i = 0; i < examUserVOList.size(); i++){
				examUser = new ExamUser();
				BeanUtils.copyProperties(examUserVOList.get(i), examUser);
				examUserHiberanteDAO.createExamUser(examUser);
//				Set<ExamRole> examRoles = new HashSet<ExamRole>();
//				examRoles.add(role);
//				examUser.setExamRoles(examRoles);
//				examUserList.add(examUser);
			}
		}
//		examUserHiberanteDAO.createExamUserList(examUserList);
	}

	@Override
	public ExamUserVO getExamUserRoleById(String userId) throws Exception {
		ExamUser examUser = examUserHiberanteDAO.getExamUserById(userId);
		ExamUserVO examUserVO = new ExamUserVO();
		if(examUser != null){
			BeanUtils.copyProperties(examUser, examUserVO);
			examUserVO.setUserRole(examUserRoleHiberanteDAO.getExamUserRoleById(userId).getExamRole().getName());
		}
		return examUserVO;
	}

	@Override
	public List<ExamUserVO> findAllExamUsersVO() {
		List<ExamUser> examUserList =  examUserHiberanteDAO.findAllExamUsers();
		List<ExamUserVO> examUserVOList = new ArrayList<ExamUserVO>();
		ExamUserVO examUserVO = null;
		for (ExamUser examUser : examUserList) {
			examUserVO = new ExamUserVO();
			BeanUtils.copyProperties(examUser,examUserVO);
			examUserVOList.add(examUserVO);
		}
		return examUserVOList;
	}

	@Override
	public List<ExamUserVO> findAllStudent25() {
		List<ExamUserRole> examUserRoleList = examUserRoleHiberanteDAO.findAllStudent25();
		List<ExamUserVO> examUserVOList = new ArrayList<ExamUserVO>();
		ExamUserVO examUserVO = null;
		for(ExamUserRole examUserRole:examUserRoleList){
			if(examUserRole.getExamRole().getName().equals("25")){
				examUserVO = new ExamUserVO();
				BeanUtils.copyProperties(examUserRole.getExamUser(),examUserVO);
				examUserVOList.add(examUserVO);
			}
		}
		return examUserVOList;
	}

}
